/*************************************************************
 * Title: HibernateBaseServiceImpl.java
 * Description: 
 * Author: Huang Shaobin
 * Email: shaobin.software@gmail.com
 * CreateTime: Feb 21, 2013 4:48:46 PM
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.biz.basic.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;

import snow.biz.basic.service.IBaseService;
import snow.biz.system.model.User;
import snow.core.context.AccessContext;
import snow.core.exceptions.BusinessException;
import snow.core.persistence.Conditions;
import snow.core.persistence.Order;
import snow.core.persistence.hibernate.HibernatePersistenceSupport;
import snow.core.security.Principal;

/**
 *
 */
public class HibernateBaseServiceImpl extends HibernatePersistenceSupport implements IBaseService {

	protected final Log log = LogFactory.getLog(getClass());

	public void delete(Object entity, LockMode lockMode) throws BusinessException {
		// TODO 需要校验行级权限的控制
		super.delete(entity, lockMode);
	}

	public <T> T get(Class<T> entityClass, Serializable id, LockMode lockMode) throws BusinessException {
		// TODO 需要校验行级权限的控制
		return super.get(entityClass, id, lockMode);
	}

	/**
	 * 获取当前用户的信息
	 * 
	 * @return
	 * @throws BusinessException
	 * @see snow.biz.basic.service.IBaseService#getCurrentUser()
	 */
	public User getCurrentUser() throws BusinessException {
		AccessContext context = AccessContext.getCurrent();
		Serializable id = context.getFromSession(Principal.CURRENT_USER_ID, Serializable.class);
		return super.get(User.class, id, null);
	}

	public <T> T load(Class<T> entityClass, Serializable id, LockMode lockMode) throws BusinessException {
		// TODO 需要校验行级权限的控制
		return super.load(entityClass, id, lockMode);
	}

	public <ID extends Serializable> ID save(Object entity, Class<ID> idClass, LockMode lockMode) throws BusinessException {
		// TODO 需要校验字段级权限的控制
		return super.save(entity, idClass, lockMode);
	}

	protected DetachedCriteria toDetachedCriteriaFrom(Conditions conditions, List<Order> orders, Class<?> entityClass) {
		// TODO 需要增加行级权限的控制
		return super.toDetachedCriteriaFrom(conditions, orders, entityClass);
	}

	public void update(Object entity, LockMode lockMode) throws BusinessException {
		// TODO 需要校验字段级权限的控制
		super.update(entity, lockMode);
	}

}
